package com.cat.dataStructure03;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/movement-of-robots/
 * @create 2025/11/8 20:25
 * @since JDK17
 */

public class Solution25 {
    public int sumDistance(int[] nums, String s, int d) {
        long ans = 0, N = (int) 1e9 + 7;
        int n = nums.length;
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == 'R') {
                arr[i] = (long) nums[i] + d;
            } else {
                arr[i] = (long) nums[i] - d;
            }
        }
        Arrays.sort(nums);  // 升序
        for (int i = 1; i < n; i++) {
            ans = (ans + (long) i * (n - i) % N * (arr[i] - arr[i - 1])) % N;
        }

        return ((int) ans);
    }
}
